package com.yisu.expressway.service_district;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.yisu.expressway.R;
import com.yisu.expressway.fragment.BaseFragment;
import com.yisu.expressway.adapter.GasListAdapter;
import com.yisu.expressway.adapter.ScenicSpotListAdapter;
import com.yisu.expressway.adapter.ServiceListAdapter;
import com.yisu.expressway.adapter.ShopListAdapter;
import com.yisu.expressway.model.ServiceDistrictItem;
import com.yisu.expressway.model.ServiceDistrictObj;
import com.yisu.expressway.network.ApiClientHelper;
import com.yisu.expressway.network.ApiResult;
import com.yisu.expressway.network.ApiSetting;
import com.yisu.expressway.ui.SlideShowView;
import com.yisu.expressway.ui.recyclerview.DividerListItemDecoration;
import com.yisu.expressway.utils.ToastUtil;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/9/29 0029.
 */
public class ServiceDistrictDetailFragment extends BaseFragment {

    @Bind(R.id.tv_title)
    protected TextView mTv_title;
    @Bind(R.id.tv_name)
    protected TextView mTv_select_district_name;
    @Bind(R.id.iv_btn_refresh)
    protected ImageButton mBtn_relocate;

//    @Bind(R.id.searching_root)
//    protected LinearLayout mSearching_root;
//    @Bind(R.id.tv_searching_notify)
//    protected TextView mTv_seaching_notify;
//    @Bind(R.id.service_district_list_root)
//    protected LinearLayout mService_district_list_root;
//    @Bind(R.id.bd_map_view)
//    protected MapView mMapView;

//    @Bind(R.id.rv_service_list)
//    protected RecyclerView mRv_list;
    @Bind(R.id.content_root)
    protected NestedScrollView mContentRoot;
    @Bind(R.id.tv_service_detail_title)
    protected TextView mServiceDetailTitle;
    @Bind(R.id.tv_good_shop)
    protected TextView mTv_goodShopTitle;
    @Bind(R.id.tv_nearby_scenic_title)
    protected TextView mTv_nearby_view_title;

    @Bind(R.id.slide_banner)
    protected SlideShowView mBannerSiler;
    @Bind(R.id.rv_gas_list)
    protected RecyclerView mRv_gas_list;
    @Bind(R.id.rv_service_sorts)
    protected RecyclerView mRv_service_list;
    @Bind(R.id.rv_shop_list)
    protected RecyclerView mRv_shop_list;
    @Bind(R.id.rv_scenic_spot_list)
    protected RecyclerView mRv_scenic_spot_list;

    private View parentView;


    private ServiceListAdapter mServiceAdapter;
    private GasListAdapter mGasAdapter;
    private ShopListAdapter mShopAdapter;
    private ScenicSpotListAdapter mScenicSpotAdapter;

    private DistrictDetailActivity mActivity;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = (DistrictDetailActivity)getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if(parentView == null) {
            parentView = inflater.inflate(R.layout.fragment_home_page, container, false);
            ButterKnife.bind(this,parentView);
            initRecyclerView();
            if(mActivity != null){
                getSelectServiceDistrictDetail(mActivity.getSelectDistrict());
            }
        }

        ViewGroup rootView = (ViewGroup) parentView.getParent();
        if (rootView != null) {
            rootView.removeView(parentView);
        }



        return parentView;
    }

    @Override
    public void onStart(){
        super.onStart();
    }



    @Override
    public void onDestroyView(){
        super.onDestroyView();
    }


    @OnClick(R.id.iv_btn_refresh)
    public void doRelocate(){
        ServiceDistrictListActivity.launch(getActivity(),true);
    }

    /**
     * 获取选中服务区详情
     */
    private void getSelectServiceDistrictDetail(ServiceDistrictItem item){
        if(item == null){
            return;
        }

        showLoading();

        Map<String, Object> params = new HashMap();
        params.put("serviceId", item.id);
        ApiClientHelper.postApi(ApiSetting.getServiceDistrictDetail(), new TypeToken<ServiceDistrictObj>() {
        }, new JSONObject(params), new Response.Listener<ApiResult<ServiceDistrictObj>>() {
            @Override
            public void onResponse(ApiResult<ServiceDistrictObj> result) {

                dismissLoading();

                parentView.setVisibility(View.VISIBLE);

                if (!result.success) {
                    ToastUtil.displayLongToastMsg(getActivity(), result.getMsg());
                    return;
                }

                initServiceDistrictView(result.getResult());


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                dismissLoading();
                ToastUtil.displayShortToastMsg(getActivity(), volleyError.getMessage());
            }
        }, this);
    }

    /**
     * 初始化recyclver View divider
     */
    private void initRecyclerView(){
        Context mContext = getActivity();


        mRv_gas_list.setLayoutManager(new LinearLayoutManager(mContext,
                LinearLayoutManager.HORIZONTAL, false));
        mRv_service_list.setLayoutManager(new LinearLayoutManager(mContext,
                LinearLayoutManager.HORIZONTAL, false));
        mRv_service_list.addItemDecoration(new DividerListItemDecoration(getActivity(),LinearLayoutManager.HORIZONTAL,4,R.color.transparent));

        mRv_shop_list.setLayoutManager(new LinearLayoutManager(mContext,
                LinearLayoutManager.HORIZONTAL, false));
        mRv_shop_list.addItemDecoration(new DividerListItemDecoration(getActivity(),LinearLayoutManager.HORIZONTAL,4,R.color.transparent));

        mRv_scenic_spot_list.setLayoutManager(new LinearLayoutManager(mContext,
                LinearLayoutManager.HORIZONTAL, false));
        mRv_scenic_spot_list.addItemDecoration(new DividerListItemDecoration(getActivity(),LinearLayoutManager.HORIZONTAL,4,R.color.transparent));
    }

    /**
     * 初始化选中的服务区UI
     * @param serviceObj 服务区详情model
     */
    private void initServiceDistrictView(ServiceDistrictObj serviceObj){



        ServiceDistrictObj.ServiceDistrictDetail mDetail = serviceObj.serviceAreaDetail;

        Context mContext = getActivity();
        mContentRoot.setVisibility(View.VISIBLE);
        mContentRoot.scrollTo(0,0);

//        mTv_select_district_name.setVisibility(View.VISIBLE);
//        mBtn_relocate.setVisibility(View.VISIBLE);
//        mBtn_scan.setVisibility(View.VISIBLE);

        mTv_title.setText(mDetail.serviceName);
        mTv_select_district_name.setText(mDetail.expressWayName);
        mBannerSiler.removeAllViews();
        mBannerSiler.initData(mContext,serviceObj.bannerList);

        if(mGasAdapter == null){
            mGasAdapter = new GasListAdapter(mContext);
        }
        mRv_gas_list.setAdapter(mGasAdapter);
        mGasAdapter.setData(mDetail.gasList);

        if(mServiceAdapter == null){
            mServiceAdapter = new ServiceListAdapter(mContext);
        }
        mRv_service_list.setAdapter(mServiceAdapter);
        mServiceAdapter.setData(mDetail.serviceItemList);
        if(mDetail.serviceItemList == null || mDetail.serviceItemList.size() == 0){
            mServiceDetailTitle.setVisibility(View.GONE);
        }else {
            mServiceDetailTitle.setVisibility(View.VISIBLE);
        }

        if(mShopAdapter == null){
            mShopAdapter = new ShopListAdapter(mContext,mDetail.id);
        }
        mRv_shop_list.setAdapter(mShopAdapter);
        mShopAdapter.setData(mDetail.shopList);
        if(mDetail.shopList == null || mDetail.shopList.size() == 0){
            mTv_goodShopTitle.setVisibility(View.GONE);
        }else {
            mTv_goodShopTitle.setVisibility(View.VISIBLE);
        }


        if(mScenicSpotAdapter == null){
            mScenicSpotAdapter = new ScenicSpotListAdapter(mContext,mDetail.id);
        }
        mRv_scenic_spot_list.setAdapter(mScenicSpotAdapter);
        mScenicSpotAdapter.setData(mDetail.viewSpotList);
        if(mDetail.viewSpotList == null || mDetail.viewSpotList.size() == 0){
            mTv_nearby_view_title.setVisibility(View.GONE);
        }else {
            mTv_nearby_view_title.setVisibility(View.VISIBLE);
        }

    }


}
